package org.example;

import Back.ReplaySelectorFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {
    private final User currentUser;

    public MainFrame(User user) {
        this.currentUser = user;
        setTitle("主界面 - " + user.getId());
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);
        getContentPane().setBackground(new Color(240, 248, 255));

        // 标题标签
        JLabel titleLabel = new JLabel("实验平台系统");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        titleLabel.setBounds(0, 20, 500, 40);
        add(titleLabel);

        // 欢迎信息
        JLabel welcomeLabel = new JLabel("欢迎您, " + user.getId() + " (" + user.getRole() + ")");
        welcomeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER);
        welcomeLabel.setBounds(0, 60, 500, 30);
        add(welcomeLabel);

        // 创建按钮
        int startY = 120;
        int buttonHeight = 40;
        int verticalSpacing = 60;

        // 根据角色显示不同功能
        String role = user.getRole();

        if ("student".equals(role)) {
            // 学生：探索实验 + 学生版实验回放
            addButton("进行探索实验", 150, startY, 200, buttonHeight, e -> startExperiment());
            addButton("实验回放(学生版)", 150, startY + verticalSpacing, 200, buttonHeight, e -> showStudentReplay());
        } else if ("teacher".equals(role)) {
            // 教师：教师版实验回放
            addButton("实验回放(教师版)", 150, startY, 200, buttonHeight, e -> showTeacherReplay());
        } else if ("admin".equals(role)) {
            // 管理员：用户管理
            addButton("用户管理", 150, startY, 200, buttonHeight, e -> openUserManagement());
        }

        // 所有角色都有退出登录
        addButton("退出登录", 150, startY + verticalSpacing * 2, 200, buttonHeight, e -> logout());
    }

    private void addButton(String text, int x, int y, int width, int height, ActionHandler handler) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.BOLD, 16));
        button.setBounds(x, y, width, height);
        button.setBackground(new Color(70, 130, 180));
        button.setForeground(Color.BLACK);
        button.setFocusPainted(false);
        button.addActionListener(handler::handle);
        add(button);
    }

    private void startExperiment() {
        JOptionPane.showMessageDialog(this, "开始探索实验...");
        // 实际实现探索实验功能
        SwingUtilities.invokeLater(() -> new MainMenu().setVisible(true));
    }

    private void showStudentReplay() {
        JOptionPane.showMessageDialog(this, "学生版实验回放功能...");
        // 实际实现学生版回放功能
        SwingUtilities.invokeLater(() -> {
            ReplaySelectorFrame frame = new ReplaySelectorFrame(currentUser);
            frame.setVisible(true);
        });
    }

    private void showTeacherReplay() {
        JOptionPane.showMessageDialog(this, "教师版实验回放功能（高级分析）...");
        // 实际实现教师版回放功能
        SwingUtilities.invokeLater(() -> {
            ReplaySelectorFrame frame = new ReplaySelectorFrame(currentUser);
            frame.setVisible(true);
        });
    }

    private void openUserManagement() {
        new UserManagementFrame(currentUser).setVisible(true);
    }

    private void logout() {
        new LoginFrame().setVisible(true);
        dispose();
    }

    @FunctionalInterface
    interface ActionHandler {
        void handle(ActionEvent e);
    }
}